﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Movies
/// </summary>
public class Movies
{
    SqlConnection conn = Utils.getDBConnection();

    private int movieID = 0;
    private string name = null;
    private string director = null;
    private string genre = null;
    private DateTime releaseDate;
    private string image = null;
    private string shortDesc = null;



    public Movies()
    {
        //
        // TODO: Add constructor logic here

    }
   
    /// <summary>
    /// /List of all getters and setter for the customer class
    /// </summary>
    public int MovieID
    {
        get { return movieID; }
        set { movieID = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string Director
    {
        get { return director; }
        set { director = value; }
    }
    public string Genre
    {
        get { return genre; }
        set { genre = value; }
    }
    public DateTime ReleaseDate
    {
        get { return releaseDate; }
        set { releaseDate = value; }
    }
    public string Image
    {
        get { return image; }
        set { image = value; }
    }
    public string ShortDesc
    {
        get { return shortDesc; }
        set { shortDesc = value; }
    }




    /// <summary>
    /// Method to get movies details given a search text
    /// </summary>
    /// <param name="user">search text</param>
    /// <returns>Movies object</returns>

    public List<Movies> getMovies(string text)
    {    

        //prepare SQL call
        SqlCommand storedProcCommand = new SqlCommand("dbo.mov_getSearchResults", conn);
        storedProcCommand.CommandType = System.Data.CommandType.StoredProcedure;
        storedProcCommand.Parameters.AddWithValue("@searchtext", text);
        conn.Open();

        //exec SQL
        SqlDataReader reader = storedProcCommand.ExecuteReader();
        List<Movies> results = new List<Movies>();


        //check 
        while (reader.Read())
        {
            Movies mv = new Movies();

            mv.MovieID = (int)reader["movie_id"];// TODO: fix 
            mv.Name = reader["movie_name"].ToString();
            mv.Director = reader["dir"].ToString();
            mv.Genre = reader["genre"].ToString();
            mv.Image = reader["movie_image"].ToString();
            mv.ShortDesc = reader["short_desc"].ToString();
            results.Add(mv);
        }

        //clean-up
        reader.Close();
        conn.Close();
        return results;
    }

    /// <summary>
    /// Method to get new movies details 
    /// </summary>
    /// <param name="user">search text</param>
    /// <returns>Movies object</returns>
    public List<Movies> getNewMovies()
    {       
        //prepare SQL call
        SqlCommand storedProcCommand = new SqlCommand("dbo.mov_getNewListings", conn);
        storedProcCommand.CommandType = System.Data.CommandType.StoredProcedure;        
        conn.Open();

        //exec SQL
        SqlDataReader reader = storedProcCommand.ExecuteReader();
        List<Movies> results = new List<Movies>();

        //check 
        while (reader.Read())
        {
            Movies mv = new Movies();
            mv.MovieID = (int)reader["movie_id"];//ODO: fix 
            mv.Name = reader["movie_name"].ToString();
            mv.Director = reader["dir"].ToString();
            mv.Genre = reader["genre"].ToString();
            mv.Image = reader["movie_image"].ToString();
            mv.ShortDesc = reader["short_desc"].ToString();
            results.Add(mv);
        }

        //clean-up
        reader.Close();
        conn.Close();
        return results;
    }

    /// <summary>
    /// Method to get movies details given a director search text
    /// </summary>
    /// <param name="user">search text</param>
    /// <returns>Movies object</returns>
    public List<Movies> getMoviesByDirector(int dirID)
    {

        //prepare SQL call
        SqlCommand storedProcCommand = new SqlCommand("dbo.mov_getMoviesByDirector", conn);
        storedProcCommand.CommandType = System.Data.CommandType.StoredProcedure;
        storedProcCommand.Parameters.AddWithValue("@movie_dir_ID", dirID);
        conn.Open();

        //exec SQL
        SqlDataReader reader = storedProcCommand.ExecuteReader();
        List<Movies> results = new List<Movies>();


        //check 
        while (reader.Read())
        {
            Movies mv = new Movies();

            mv.MovieID = (int)reader["movie_id"];
            mv.Name = reader["movie_name"].ToString();
            mv.Director = reader["dir"].ToString();
            mv.Genre = reader["genre"].ToString();
            mv.Image = reader["movie_image"].ToString();
            mv.ShortDesc = reader["short_desc"].ToString();
            results.Add(mv);
        }

        //clean-up
        reader.Close();
        conn.Close();
        return results;
    }

    /// <summary>
    /// Method to get movies details given a genre search text
    /// </summary>
    /// <param name="user">search text</param>
    /// <returns>Movies object</returns>
    public List<Movies> getMoviesByGenre(int genreID)
    {

        //prepare SQL call
        SqlCommand storedProcCommand = new SqlCommand("dbo.mov_getMoviesByGenre", conn);
        storedProcCommand.CommandType = System.Data.CommandType.StoredProcedure;
        storedProcCommand.Parameters.AddWithValue("@movie_genre_ID", genreID);
        conn.Open();

        //exec SQL
        SqlDataReader reader = storedProcCommand.ExecuteReader();
        List<Movies> results = new List<Movies>();


        //check 
        while (reader.Read())
        {
            Movies mv = new Movies();

            mv.MovieID = (int)reader["movie_id"];
            mv.Name = reader["movie_name"].ToString();
            mv.Director = reader["dir"].ToString();
            mv.Genre = reader["genre"].ToString();
            mv.Image = reader["movie_image"].ToString();
            mv.ShortDesc = reader["short_desc"].ToString();
            results.Add(mv);
        }

        //clean-up
        reader.Close();
        conn.Close();
        return results;
    }

    /// <summary>
    /// Method to save movie to queue 
    /// </summary>
    /// <param name="user">search text</param>
    /// <returns>Movies object</returns>
    public int saveQueue(string cusEmail, int movieID)
    {
        try
        {
            //prepare SQL call
            SqlCommand storedProcCommand = new SqlCommand("dbo.mov_addToQueue", conn);
            storedProcCommand.CommandType = System.Data.CommandType.StoredProcedure;
            storedProcCommand.Parameters.AddWithValue("@cus_email", cusEmail);
            storedProcCommand.Parameters.AddWithValue("@movie_id", movieID);
            conn.Open();

            //exec SQL
            int row = storedProcCommand.ExecuteNonQuery();

            conn.Close();
            return row;
        }
        catch (SqlException ex)
        {
            conn.Close();
            Console.WriteLine("Error inserting queue data:" + ex.Message);
            return 99;
        }

    }

}